﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Transport.Admin
{
    public partial class TraCuuXe : System.Web.UI.Page
    {
        XeControl xeCtrl;
        PhanCongControl pcCtrl;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                xeCtrl = new XeControl();
                pcCtrl = new PhanCongControl();
                List<BGTLTentity> lslt = xeCtrl.listAllByRoute();
                List<BGTNentity> lsn = xeCtrl.listAllByDay();

                if (txt_Begin.Text != "" && txt_End.Text != "")
                {
                    DateTime begin = DateTime.Parse(txt_Begin.Text);
                    DateTime end = DateTime.Parse(txt_End.Text);

                    for (int i = lslt.Count - 1; i >= 0; i--)
                    {
                        if (pcCtrl.checkCarStatus(lslt[i].Ma_bien_so, begin, end) == false)
                            lslt.RemoveAt(i);
                    }
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (pcCtrl.checkCarStatus(lsn[i].Bien_so, begin, end) == false)
                            lsn.RemoveAt(i);
                    }
                }

                string lotrinh = ddl_LT.SelectedItem.Text;
                if (lotrinh != "Select All")
                {
                    for (int i = lslt.Count - 1; i >= 0; i--)
                    {
                        if (lslt[i].Ten_lo_trinh != lotrinh)
                            lslt.RemoveAt(i);
                    }
                }

                string dichvu = ddl_DV.SelectedItem.Text;
                if (dichvu != "Select All")
                {
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (lsn[i].Ten_dich_vu != dichvu)
                            lsn.RemoveAt(i);
                    }
                }

                string region = ddl_Mien.SelectedItem.Text;
                if (region != "Select All")
                {
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (lsn[i].Ten_mien != region)
                            lsn.RemoveAt(i);
                    }
                }

                string thuonghieu = ddl_Brand.SelectedItem.Text;
                if (thuonghieu != "Select All")
                {
                    for (int i = lslt.Count - 1; i >= 0; i--)
                    {
                        if (lslt[i].Ten_thuong_hieu != thuonghieu)
                            lslt.RemoveAt(i);
                    }
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (lsn[i].Ten_thuong_hieu != thuonghieu)
                            lsn.RemoveAt(i);
                    }
                }

                string tinhtrang = ddl_Status.SelectedItem.Text;
                if (tinhtrang != "Select All")
                {
                    for (int i = lslt.Count - 1; i >= 0; i--)
                    {
                        if (lslt[i].Tinh_trang != tinhtrang)
                            lslt.RemoveAt(i);
                    }
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (lsn[i].Tinh_trang != tinhtrang)
                            lsn.RemoveAt(i);
                    }
                }

                if (txt_Nfrom.Text != "" && txt_Nto.Text != "")
                {
                    int ifrom = Int32.Parse(txt_Nfrom.Text);
                    int ito = Int32.Parse(txt_Nto.Text);

                    for (int i = lslt.Count - 1; i >= 0; i--)
                    {
                        if (lslt[i].So_cho < ifrom || lslt[i].So_cho > ito)
                            lslt.RemoveAt(i);
                    }
                    for (int i = lsn.Count - 1; i >= 0; i--)
                    {
                        if (lsn[i].So_cho < ifrom || lsn[i].So_cho > ito)
                            lsn.RemoveAt(i);
                    }
                }

                GridView_LT.DataSource = lslt;
                GridView_LT.DataBind();
                GridView_N.DataSource = lsn;
                GridView_N.DataBind();
            }
            catch (Exception)
            {

            }
        }
    }
}